import Content from '@/sheep/layouts/content.vue';
import { request } from '@/sheep/request';
import { SELECT, CRUD, RECYCLE } from '@/sheep/request/crud';
import { baseURL } from '@/sheep/config';

const route = {
  path: 'decorate',
  name: 'shop.admin.decorate',
  component: Content,
  meta: {
    title: '店铺装修',
  },
  children: [
    {
      path: 'template',
      name: 'shop.admin.decorate.template',
      component: () => import('./template/index.vue'),
      meta: {
        title: '店铺模板',
      },
    },
    {
      path: 'page',
      name: 'shop.admin.decorate.page',
      component: () => import('./page/index.vue'),
      meta: {
        title: '页面装修',
      },
    },
    {
      path: 'designer',
      name: 'shop.admin.decorate.designer',
      component: () => import('./designer/index.vue'),
      meta: {
        title: '设计师模板',
      },
    },
  ],
};

const api = {
  getPage: (id, type) =>
    request({
      url: 'shop/admin/decorate/page/' + id,
      method: 'GET',
      params: {
        type,
      },
    }),
  updatePage: (id, type, data) =>
    request({
      url: 'shop/admin/decorate/page/' + id,
      method: 'PUT',
      params: {
        type,
      },
      data,
      options: {
        showSuccessMessage: true,
      },
    }),
  template: {
    ...CRUD('shop/admin/decorate/template'),
    ...RECYCLE('shop/admin/decorate/template'),
    select: (params) => SELECT('shop/admin/decorate/template', params),
    copy: (id) =>
      request({
        url: 'shop/admin/decorate/template/copy/' + id,
        method: 'POST',
        options: {
          showSuccessMessage: true,
        },
      }),
    status: (id, data) =>
      request({
        url: 'shop/admin/decorate/template/status/' + id,
        method: 'POST',
        data,
        options: {
          showSuccessMessage: true,
        },
      }),
  },
  // 设计师模板
  designer: {
    list: () =>
      request({
        url: 'https://api.sheepjs.com/api/designer',
        method: 'GET',
      }),
    use: async (id) => {
      const { error, data } = await request({
        url: 'https://api.sheepjs.com/api/designer/' + id,
        method: 'GET',
      });
      if (error === 0) {
        request({
          url: 'shop/admin/decorate/designer/use',
          method: 'POST',
          data,
          options: {
            showSuccessMessage: true,
          },
        });
      }
    },
  },
  getWxacode: (path) =>
    `${baseURL}/shop/api/third/wechat/wxacode?platform=miniProgram&payload=${encodeURIComponent(
      JSON.stringify({
        path,
      }),
    )}`,
};

export { route, api };
